package com.haltakov.letterjumble.ui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.haltakov.letterjumble.Messages;
import com.haltakov.letterjumble.game.api.GameController;

public class SettingsDialog extends JDialog {

	public SettingsDialog(GameController game) {

		super();
		setLayout(new GridLayout(2,1));
		setPreferredSize(new Dimension(300,200));

		//FIRST ROW - GAME LANGUAGE
		JPanel firstRowPanel = new JPanel(new FlowLayout());
		firstRowPanel.setPreferredSize(new Dimension(300, 100));
		firstRowPanel.setLayout(new FlowLayout());

		JLabel gameLanguageLabel = new JLabel(Messages.getString("SettingsDialog.language")); //$NON-NLS-1$
		gameLanguageLabel.setBorder(BorderFactory.createEmptyBorder(30, 0, 0, 15));
		firstRowPanel.add(gameLanguageLabel);

		DefaultComboBoxModel comboModel = new DefaultComboBoxModel();
		JComboBox languagesList = new JComboBox(comboModel);
		
		File dataDir = new File(game.getDataDirectory());
		
		for (File f: dataDir.listFiles()) {
			if (f.isDirectory()) {
				Locale l = new Locale(f.getName());
				comboModel.addElement(l.getDisplayLanguage() + " - " + l.getLanguage()); //$NON-NLS-1$
			}
		}
		
		languagesList.setBorder(BorderFactory.createEmptyBorder(30, 0, 0, 0));
		firstRowPanel.add(languagesList);

		add(firstRowPanel);

		//SECOND ROW - BUTTONS
		JPanel secondRowPanel = new JPanel(new FlowLayout());
		secondRowPanel.setPreferredSize(new Dimension(300, 100));
		secondRowPanel.setLayout(new FlowLayout());

		JButton okButton = new JButton(Messages.getString("SettingsDialog.ok")); //$NON-NLS-1$
		secondRowPanel.add(okButton);

		JButton cancelButton = new JButton(Messages.getString("SettingsDialog.cancel")); //$NON-NLS-1$
		secondRowPanel.add(cancelButton);

		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				SettingsDialog.this.setVisible(false);
			}
		});

		add(secondRowPanel);

		setTitle(Messages.getString("SettingsDialog.settings")); //$NON-NLS-1$

		pack();
	}

}